package user

import (
	"context"
	"encoding/json"
	"errors"
	"hzjt-oam/rpc/sys/pb"

	"hzjt-oam/admin/internal/svc"
	"hzjt-oam/admin/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type UserDeleteLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewUserDeleteLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserDeleteLogic {
	return &UserDeleteLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *UserDeleteLogic) UserDelete(req *types.DeleteUserReq) (resp *types.DeleteUserResp, err error) {
	userId, err := l.ctx.Value("userId").(json.Number).Int64()
	if err != nil {
		return nil, errors.New("验证token未通过")
	}
	user_one, err := l.svcCtx.UserSrv.GetSysUserById(l.ctx, &pb.GetSysUserByIdReq{
		Id: int64(userId),
	})
	if user_one.SysUser.RoleName == "超级管理员" {
		l.svcCtx.UserSrv.DelSysUser(l.ctx, &pb.DelSysUserReq{Id: req.Id})
		return &types.DeleteUserResp{Message: "成功"}, nil
	}
	return
}
